package com.inray.nbs.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.ProtocolException;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.inray.nbs.bean.Userinfo;
import com.inray.nbs.bean.Vehicle_bat_single_temp;
import com.inray.nbs.bean.Vehicle_bat_single_vol;
import com.inray.nbs.bean.Vehicle_data;
import com.inray.nbs.bean.Vehicle_gps_data;
import com.inray.nbs.bean.Vehicle_info;
import com.inray.nbs.bean.Vehicle_location;
import com.inray.nbs.bean.Vehicle_warning_data;
import com.inray.nbs.service.FleetService;
import com.inray.nbs.service.VehicleService;
import com.inray.nbs.utils.MapUtil;
import com.inray.nbs.vo.City;
import com.inray.nbs.vo.PlayBack;


/**
 * 车辆控制类
 * VehicleController
 * @author zhang
 *
 */
@Controller
@RequestMapping(value = "/vehicle")
public class VehicleController {
	@Resource
	private VehicleService vService;
	@Resource
	private FleetService fleetService;
	/**
	 * 查询车辆GPS信息
	 * @param request
	 * @param vehicleid
	 * @return
	 */
	@RequestMapping(value = "/selectGpsByOne.do", method=RequestMethod.POST  )
	@ResponseBody
	public String selectGpsByOne(HttpServletRequest request,Integer vehicleid){
		Map<String,Object> map = new HashMap<String,Object>();
		if(vehicleid!=null){
			map.put("vehicleid", vehicleid);
		}else{
			Userinfo user = (Userinfo)request.getSession().getAttribute("user");	
			map.put("userid", user.getUserid());
			String vehicle_name=request.getSession().getAttribute("vehicle_name").toString();
			String typeid=request.getSession().getAttribute("typeid").toString();
			String fleetid=request.getSession().getAttribute("fleetid").toString();
			if(vehicle_name!=null&&!vehicle_name.equals("")){
				map.put("vehicle_name", "%"+vehicle_name+"%");
			}
			if(typeid!=null&&!typeid.equals("")){
				map.put("typeid", Integer.parseInt(typeid));
			}
			if(fleetid!=null&&!fleetid.equals("")){
				map.put("fleetid", Integer.parseInt(fleetid));
			}
		}
		List<Vehicle_gps_data> list = vService.selectGpsByOne(map);
		MapUtil mapUtil=new MapUtil();
		List<Vehicle_gps_data> rslst= new ArrayList<Vehicle_gps_data>();
		for(Vehicle_gps_data v:list){
			List<Double> l=mapUtil.transform2baidu_each(v.getLatitude(), v.getLongitude());
			v.setLatitude(l.get(0));
			v.setLongitude(l.get(1));
			rslst.add(v);
		}
		return JSONArray.fromObject(rslst).toString();
	}
	/**
	 * 查询车辆省市位置信息
	 * @param request
	 * @param vehicleid
	 * @return
	 */
	@RequestMapping(value = "/selectVehicleLocationByUser.do", method=RequestMethod.GET  )
	@ResponseBody
	public String selectVehicleLocationByOne(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		Userinfo user = (Userinfo)request.getSession().getAttribute("user");	
		if(user.getUserid()!=0){//判断是否为系统管理员 否则根据用户id加载车辆
			map.put("userid", user.getUserid());
		}

		Object vehicle_name=request.getSession().getAttribute("vehicle_name");
		Object typeid=request.getSession().getAttribute("typeid");
		Object fleetid=request.getSession().getAttribute("fleetid");
		if(vehicle_name!=null&&!vehicle_name.equals("")){
			map.put("vehicle_name", "%"+vehicle_name.toString()+"%");
		}
		if(typeid!=null&&!typeid.equals("")){
			map.put("typeid", Integer.parseInt(typeid.toString()));
		}
		if(fleetid!=null&&!fleetid.equals("")){
			map.put("fleetid", Integer.parseInt(fleetid.toString()));
		}
		
		List<Vehicle_location> list = vService.selectVehicleLocationByUser(map);
		List<City> plst= new ArrayList<City>();//省级坐标集
		for(Vehicle_location v:list){
			if(plst.size()>0){
				int ishave = 0;
				for(City c1:plst){
					if(c1.getProvince().equals(v.getProvince())){
						ishave=1;
						break;
					}
				}
				if(ishave==0){
					City c = new City();
					c.setProvince(v.getProvince());
					StringBuilder builder = null;
					URL url;
					String gpsUrl ="http://api.map.baidu.com/geocoder/v2/?address="+c.getProvince()+"&output=json&ak=7bfcb05227f0f4324410e3b17fe3ea00&callback=showLocation";
					try {
						url = new URL(gpsUrl);
						HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
						// 设置连接超时
						httpConn.setConnectTimeout(5 * 1000);
						httpConn.setRequestMethod("GET");
						builder = new StringBuilder();
						BufferedReader reader = new BufferedReader(new InputStreamReader(
								httpConn.getInputStream(), "utf-8"));
						String s = null;
						while ((s = reader.readLine()) != null) {
							builder.append(s);
						}
						String str = builder.toString();
						str=str.substring(27, str.length()-1);
						org.json.JSONObject  dataJson=new org.json.JSONObject(str);
						org.json.JSONObject  result=dataJson.getJSONObject("result");
						org.json.JSONObject location=result.getJSONObject("location");
						Double lng=location.getDouble("lng");
						Double lat=location.getDouble("lat");
						c.setPlong(lng);
						c.setPlat(lat);
					} catch (MalformedURLException e1) {
						e1.printStackTrace();
					} catch (ProtocolException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					plst.add(c);
				}
			}else{
				City c = new City();
				c.setProvince(v.getProvince());
				StringBuilder builder = null;
				URL url;
				String gpsUrl ="http://api.map.baidu.com/geocoder/v2/?address="+c.getProvince()+"&output=json&ak=7bfcb05227f0f4324410e3b17fe3ea00&callback=showLocation";
				try {
					url = new URL(gpsUrl);
					HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
					// 设置连接超时
					httpConn.setConnectTimeout(5 * 1000);
					httpConn.setRequestMethod("GET");
					builder = new StringBuilder();
					BufferedReader reader = new BufferedReader(new InputStreamReader(
							httpConn.getInputStream(), "utf-8"));
					String s = null;
					while ((s = reader.readLine()) != null) {
						builder.append(s);
					}
					String str = builder.toString();
					str=str.substring(27, str.length()-1);
					org.json.JSONObject  dataJson=new org.json.JSONObject(str);
					org.json.JSONObject  result=dataJson.getJSONObject("result");
					org.json.JSONObject location=result.getJSONObject("location");
					Double lng=location.getDouble("lng");
					Double lat=location.getDouble("lat");
					c.setPlong(lng);
					c.setPlat(lat);
				} catch (MalformedURLException e1) {
					e1.printStackTrace();
				} catch (ProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				plst.add(c);
			}
		}
		List<City> pclst= new ArrayList<City>();//省市级坐标集
		for(City c:plst){
			List<City> clst= new ArrayList<City>();//市级坐标集
			for(Vehicle_location v:list){
				if(v.getProvince().equals(c.getProvince())){
					if(clst.size()>0){
						int ishave=0;
						for(City c1:clst){
							if(c1.getCity().equals(v.getCity())){
								ishave=1;
								break;
							}
						}
						if(ishave==0){
							City city = new City();
							city.setProvince(c.getProvince());
							city.setCity(v.getCity());
							StringBuilder builder = null;
							URL url;
							String gpsUrl ="http://api.map.baidu.com/geocoder/v2/?address="+city.getCity()+"&output=json&ak=7bfcb05227f0f4324410e3b17fe3ea00&callback=showLocation";
							try {
								url = new URL(gpsUrl);
								HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
								// 设置连接超时
								httpConn.setConnectTimeout(5 * 1000);
								httpConn.setRequestMethod("GET");
								builder = new StringBuilder();
								BufferedReader reader = new BufferedReader(new InputStreamReader(
										httpConn.getInputStream(), "utf-8"));
								String s = null;
								while ((s = reader.readLine()) != null) {
									builder.append(s);
								}
								String str = builder.toString();
								str=str.substring(27, str.length()-1);
								org.json.JSONObject  dataJson=new org.json.JSONObject(str);
								org.json.JSONObject  result=dataJson.getJSONObject("result");
								org.json.JSONObject location=result.getJSONObject("location");
								Double lng=location.getDouble("lng");
								Double lat=location.getDouble("lat");
								city.setClong(lng);
								city.setClat(lat);
							} catch (MalformedURLException e1) {
								e1.printStackTrace();
							} catch (ProtocolException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							}
							clst.add(city);
						}
					}else{
						City city = new City();
						city.setProvince(c.getProvince());
						city.setCity(v.getCity());
						StringBuilder builder = null;
						URL url;
						String gpsUrl ="http://api.map.baidu.com/geocoder/v2/?address="+city.getCity()+"&output=json&ak=7bfcb05227f0f4324410e3b17fe3ea00&callback=showLocation";
						try {
							url = new URL(gpsUrl);
							HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
							// 设置连接超时
							httpConn.setConnectTimeout(5 * 1000);
							httpConn.setRequestMethod("GET");
							builder = new StringBuilder();
							BufferedReader reader = new BufferedReader(new InputStreamReader(
									httpConn.getInputStream(), "utf-8"));
							String s = null;
							while ((s = reader.readLine()) != null) {
								builder.append(s);
							}
							String str = builder.toString();
							str=str.substring(27, str.length()-1);
							org.json.JSONObject  dataJson=new org.json.JSONObject(str);
							org.json.JSONObject  result=dataJson.getJSONObject("result");
							org.json.JSONObject location=result.getJSONObject("location");
							Double lng=location.getDouble("lng");
							Double lat=location.getDouble("lat");
							city.setClong(lng);
							city.setClat(lat);
						} catch (MalformedURLException e1) {
							e1.printStackTrace();
						} catch (ProtocolException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
						clst.add(city);
					}
				}
			}
			c.setClst(clst);
			pclst.add(c);
		}
		List<City> rspclst= new ArrayList<City>();//最终返回结果集
		for(City c:pclst){
			City newc = new City();
			newc.setProvince(c.getProvince());
			newc.setPlong(c.getPlong());
			newc.setPlat(c.getPlat());
			List<City> cilst = c.getClst();
			List<City> clst = new ArrayList<City>();
			for(City city:cilst){
				List<Vehicle_location> vlst= new ArrayList<Vehicle_location>();
				for(Vehicle_location v:list){
					if(c.getProvince().equals(v.getProvince())&&city.getCity().equals(v.getCity())){
						vlst.add(v);
					}
				}
				city.setVlst(vlst);
				clst.add(city);
			}
			newc.setClst(clst);
			rspclst.add(newc);
		}
		System.out.println(JSONArray.fromObject(rspclst).toString());
		return JSONArray.fromObject(rspclst).toString();
	}
	/**
	 * 查询各省市车辆数量
	 * @param request
	 * @param vehicleid
	 * @return
	 */
	@RequestMapping(value = "/selectLocationVehicleByUser.do", method=RequestMethod.GET  )
	@ResponseBody
	public String selectLocationVehicleByUser(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		Userinfo user = (Userinfo)request.getSession().getAttribute("user");	
		if(user.getUserid()!=0){//判断是否为系统管理员 否则根据用户id加载车辆
			map.put("userid", user.getUserid());
		}
		Object vehicle_name=request.getSession().getAttribute("vehicle_name");
		Object typeid=request.getSession().getAttribute("typeid");
		Object fleetid=request.getSession().getAttribute("fleetid");
		if(vehicle_name!=null&&!vehicle_name.equals("")){
			map.put("vehicle_name", "%"+vehicle_name.toString()+"%");
		}
		if(typeid!=null&&!typeid.equals("")){
			map.put("typeid", Integer.parseInt(typeid.toString()));
		}
		if(fleetid!=null&&!fleetid.equals("")){
			map.put("fleetid", Integer.parseInt(fleetid.toString()));
		}
		
		List<Vehicle_location> list_province = vService.selectLocationVehicleByUser_province(map);
		List<Vehicle_location> list_city = vService.selectLocationVehicleByUser_city(map);
		JSONObject result = new JSONObject();
		for(Vehicle_location vl_p:list_province){
			result.put(vl_p.getProvince(), vl_p.getVehicle_num());	
		}
		for(Vehicle_location vl_c:list_city){
			result.put(vl_c.getCity(),vl_c.getVehicle_num());
		}
		return JSONArray.fromObject(result).toString();
	}
	/**
	 * 查询各省车辆数量
	 * @param request
	 * @param vehicleid
	 * @return
	 */
	@RequestMapping(value = "/selectProvinceVehicleByUser.do", method=RequestMethod.POST  )
	@ResponseBody
	public String selectProvinceVehicleByUser(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		Userinfo user = (Userinfo)request.getSession().getAttribute("user");	
		if(user.getUserid()!=0){//判断是否为系统管理员 否则根据用户id加载车辆
			map.put("userid", user.getUserid());
		}
		Object vehicle_name=request.getSession().getAttribute("vehicle_name");
		Object typeid=request.getSession().getAttribute("typeid");
		Object fleetid=request.getSession().getAttribute("fleetid");
		if(vehicle_name!=null&&!vehicle_name.equals("")){
			map.put("vehicle_name", "%"+vehicle_name.toString()+"%");
		}
		if(typeid!=null&&!typeid.equals("")){
			map.put("typeid", Integer.parseInt(typeid.toString()));
		}
		if(fleetid!=null&&!fleetid.equals("")){
			map.put("fleetid", Integer.parseInt(fleetid.toString()));
		}
		
		List<Vehicle_location> list_province = vService.selectLocationVehicleByUser_province(map);
		Map<String,Object> map_list=new HashMap<String, Object>();
		for(Vehicle_location vl_p:list_province){
			System.out.println(vl_p.getProvince()+"    "+vl_p.getVehicle_num());
			map_list.put(vl_p.getProvince(), vl_p.getVehicle_num());	
		}
		return JSONArray.fromObject(list_province).toString();
	}
	/**
	 * 查询各市车辆数量
	 * @param request
	 * @param vehicleid
	 * @return
	 */
	@RequestMapping(value = "/selectCityVehicleByUser.do", method=RequestMethod.POST  )
	@ResponseBody
	public String selectCityVehicleByUser(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		Userinfo user = (Userinfo)request.getSession().getAttribute("user");	
		if(user.getUserid()!=0){//判断是否为系统管理员 否则根据用户id加载车辆
			map.put("userid", user.getUserid());
		}
		Object vehicle_name=request.getSession().getAttribute("vehicle_name");
		Object typeid=request.getSession().getAttribute("typeid");
		Object fleetid=request.getSession().getAttribute("fleetid");
		if(vehicle_name!=null&&!vehicle_name.equals("")){
			map.put("vehicle_name", "%"+vehicle_name.toString()+"%");
		}
		if(typeid!=null&&!typeid.equals("")){
			map.put("typeid", Integer.parseInt(typeid.toString()));
		}
		if(fleetid!=null&&!fleetid.equals("")){
			map.put("fleetid", Integer.parseInt(fleetid.toString()));
		}
		
		List<Vehicle_location> list_city = vService.selectLocationVehicleByUser_city(map);
		JSONObject result = new JSONObject();
		for(Vehicle_location vl_c:list_city){
			result.put(vl_c.getCity(),vl_c.getVehicle_num());
		}
		return JSONArray.fromObject(result).toString();
	}
	/**
	 * 更新车辆省市信息
	 * @param request
	 * @param vehicleid
	 * @return
	 */
	@RequestMapping(value = "/updateVehicleLocation.do", method=RequestMethod.GET  )
	@ResponseBody
	public String updateVehicleLocation(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		MapUtil mapUtil=new MapUtil();
		List<Vehicle_gps_data> list = vService.selectGpsByOne(map);
		List<Vehicle_location> rslst= new ArrayList<Vehicle_location>();
		for(Vehicle_gps_data v:list){
			Vehicle_location vl=new Vehicle_location();
			List<Double> l=mapUtil.transform2baidu_each(v.getLatitude(), v.getLongitude());
			v.setLatitude(l.get(0));
			v.setLongitude(l.get(1));
			//车辆所属省份及城市
			StringBuilder builder = null;
			URL url;
			String locations=v.getLatitude()+","+v.getLongitude();
			String gpsUrl ="http://api.map.baidu.com/geocoder/v2/?ak=7bfcb05227f0f4324410e3b17fe3ea00&callback=renderReverse&location="+locations+"&output=json&pois=0";
			try {
				url = new URL(gpsUrl);
				HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
				// 设置连接超时
				httpConn.setConnectTimeout(5 * 1000);
				httpConn.setRequestMethod("GET");
				builder = new StringBuilder();
				BufferedReader reader = new BufferedReader(new InputStreamReader(
						httpConn.getInputStream(), "utf-8"));
				String s = null;
				while ((s = reader.readLine()) != null) {
					builder.append(s);
				}
				String str = builder.toString();
				str=str.substring(29, str.length()-1);
				
				org.json.JSONObject  dataJson=new org.json.JSONObject(str);
				org.json.JSONObject  result=dataJson.getJSONObject("result");
				org.json.JSONObject info=result.getJSONObject("addressComponent");
				String province=info.getString("province");
				if(province.indexOf("省")!=-1){
					province=province.substring(0, province.length()-1);
				}
				String city=info.getString("city");
				vl.setProvince(province);
				vl.setCity(city);
				vl.setLatitude(v.getLatitude());
				vl.setLongitude(v.getLongitude());
				vl.setVin(v.getVin());
				vl.setVehicle_name(v.getVehicle_name());
				rslst.add(vl);
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (ProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		vService.updateVehicleLocation(rslst);
		return null;	
	}
	/**
	 * 查询车辆状态
	 * @param request
	 * @param vehicleid
	 * @return
	 */
	@RequestMapping(value = "/selectVehicleStatusByUser.do", method=RequestMethod.POST  )
	@ResponseBody
	public String selectVehicleStatusByOne(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		Userinfo user = (Userinfo)request.getSession().getAttribute("user");	
		if(user.getUserid()!=0){//判断是否为系统管理员 否则根据用户id加载车辆
			map.put("userid", user.getUserid());
		}
		Object vehicle_name=request.getSession().getAttribute("vehicle_name");
		Object typeid=request.getSession().getAttribute("typeid");
		Object fleetid=request.getSession().getAttribute("fleetid");
		if(vehicle_name!=null&&!vehicle_name.equals("")){
			map.put("vehicle_name", "%"+vehicle_name.toString()+"%");
		}
		if(typeid!=null&&!typeid.equals("")){
			map.put("typeid", Integer.parseInt(typeid.toString()));
		}
		if(fleetid!=null&&!fleetid.equals("")){
			map.put("fleetid", Integer.parseInt(fleetid.toString()));
		}
		List<Vehicle_data> list = vService.selectVehicleStatusByUser(map);
		int count_online=0;
		int count_vehicle=0;
		int count_travel=0;
		int count_charging=0;
		int count_err=0;
		for(Vehicle_data vd:list){
			if(vd.getState().equals(1)){
				count_online++;
				if(vd.getWarning_flag()!=null&&vd.getWarning_flag()>0){
					count_err++;
				}else if(vd.getVehicle_speed()!=null&&vd.getVehicle_speed()>0){
					count_travel++;
				}else if(vd.getCharging_status()!=null&&(vd.getCharging_status().equals(1)||vd.getCharging_status().equals(2))){
					count_charging++;
				}
			}
		}
		count_vehicle=list.size();
		Map<String,Object> map_list=new HashMap<String, Object>();
		map_list.put("count_vehicle", count_vehicle);
		map_list.put("count_online", count_online);
		map_list.put("count_charging", count_charging);
		map_list.put("count_travel", count_travel);
		map_list.put("count_err", count_err);
		map_list.put("count_offline", count_vehicle-count_online);
		map_list.put("count_stop", count_online-count_travel-count_err-count_charging);
		return JSONArray.fromObject(map_list).toString();
	}
	
	/**
	 * 查询单个车辆实时运营信息
	 * @param request
	 * @param vehicleid
	 * @return
	 */
	@RequestMapping(value = "/selectAllByOne.do", method=RequestMethod.GET  )
	@ResponseBody
	public String selectAllByOne(HttpServletRequest request,Integer vehicleid){
		Map<String,Object> map = new HashMap<String,Object>();
		if(vehicleid!=null){
			map.put("vehicleid", vehicleid);
		}
		Vehicle_info v = vService.selectAllByOne(map);
		MapUtil mapUtil=new MapUtil();
		if(v.getLongitude()!=null&&v.getLatitude()!=null&&v.getLongitude()!=0&&v.getLatitude()!=0){
			//GPS坐标转换
			List<Double> l=mapUtil.transform2baidu_each(v.getLatitude(), v.getLongitude());
			v.setLatitude(l.get(0));
			v.setLongitude(l.get(1));
		}
		List<Vehicle_bat_single_vol> bat_vol_list= vService.selectBatVolByOne(map);
		List<Vehicle_bat_single_temp> bat_temp_list=vService.selectBatTempByOne(map);
		
		v.setBat_vol_list(bat_vol_list);
		v.setBat_temp_list(bat_temp_list);
		
		return JSONArray.fromObject(v).toString();
	}
	/**
	 * 查询是否有车辆报警
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/selectVehicleIsAlert.do", method=RequestMethod.POST  )
	@ResponseBody
	public int selectVehicleIsAlert(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		Userinfo user = (Userinfo)request.getSession().getAttribute("user");
		map.put("userid", user.getUserid());
		List<Vehicle_warning_data> list = vService.selectVehicleIsAlert(map);
		return list.size();
	}
	/**
	 * 查询车辆实时报警详细信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/selectVehicleWarnning.do", method=RequestMethod.POST  )
	@ResponseBody
	public int selectVehicleWarnning(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		Userinfo user = (Userinfo)request.getSession().getAttribute("user");
		map.put("userid", user.getUserid());
		List<Vehicle_warning_data> list = vService.selectVehicleIsAlert(map);
		String bms_err="";
		for(Vehicle_warning_data vw:list){
			int warning_flag=vw.getWarning_flag();
			if(warning_flag%2==1){
				bms_err+="温度差异报警;";
			}
			if((warning_flag>>1)%2==1){
				bms_err+="电池高温报警;";
			}
			if((warning_flag>>2)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>3)%2==1){
				bms_err+="车载储能装置欠压报警;";
			}
			if((warning_flag>>4)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>5)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>6)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>7)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>8)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>9)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>10)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>11)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>12)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>13)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>14)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>15)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>16)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
			if((warning_flag>>17)%2==1){
				bms_err+="车载储能装置过压报警;";
			}
		}
		return list.size();
	}
	/**
	 * 查询单个车辆轨迹回放
	 * @param request
	 * @param vehicleid
	 * @param ksrq
	 * @param jsrq
	 * @return
	 */
	@RequestMapping(value = "/selectPlayBackByOne.do", method=RequestMethod.POST  )
	@ResponseBody
	public String selectPlayBackByOne(HttpServletRequest request,Integer vehicleid,String ksrq,String jsrq){
		Map<String,Object> map = new HashMap<String,Object>();
		if(vehicleid!=null){
			map.put("vehicleid", vehicleid);
		}
		if(ksrq!=null&&!"".equals(ksrq)){
			map.put("ksrq", ksrq);
		}
		if(jsrq!=null&&!"".equals(jsrq)){
			map.put("jsrq", jsrq);
		}
		List<Vehicle_gps_data> list = vService.selectPlayBackByOne(map);
		PlayBack back = new PlayBack();
		if(list.size()>0){
			back.setVehicleid(list.get(0).getVehicleid());
			back.setVehiclename(list.get(0).getVehicle_name());
			back.setVin(list.get(0).getVin());
			
			List<String> lastdate=new ArrayList<String>();
			List<Double> gpslng=new ArrayList<Double>();
			List<Double> gpslat=new ArrayList<Double>();
			List<Double> vehicleSpeed=new ArrayList<Double>();
			for(Vehicle_gps_data g:list){
				MapUtil mapUtil=new MapUtil();
				if(g.getLongitude()!=null&&g.getLatitude()!=null&&g.getLongitude()!=0&&g.getLatitude()!=0){
					//GPS坐标转换
					List<Double> l=mapUtil.transform2baidu_each(g.getLatitude(), g.getLongitude());
					gpslng.add(l.get(1));
					gpslat.add(l.get(0));
					lastdate.add(g.getSensor_time());
					vehicleSpeed.add(g.getSpeed()*1.0);
				}
			}
			back.setLastdate(lastdate);
			back.setVehicleSpeed(vehicleSpeed);
			back.setGpslat(gpslat);
			back.setGpslng(gpslng);
			}
		return JSONArray.fromObject(back).toString();
	}
}
